package com.jic.market.impl;

import java.lang.Long;
import java.math.BigDecimal;
import java.util.List;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.entity.BargainLog;
import com.jic.market.mapper.PromotionBargainLogMapper;
import com.jic.market.request.rest.bargain.MyBargainRestRequest;
import com.jic.market.response.rest.bargain.BargainLogRestResponse;
import com.jic.market.service.BargainLogService;
import com.jic.member.api.rest.MememberApi;
import com.jic.member.response.MememberUserResponse;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 砍价记录表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-09-10 06:20:18
 */
@Service
public class BargainLogServiceImpl implements BargainLogService {

    @Autowired
    private PromotionBargainLogMapper bargainLogMapper;

    @Autowired
    private MememberApi mememberApi;

    @Override
    public long insert(BargainLog bargainLog) {
        long flag = bargainLogMapper.insert(bargainLog);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return bargainLogMapper.delete(id);
    }

    @Override
    public long update(BargainLog bargainLog) {
        return bargainLogMapper.update(bargainLog);
    }

    @Override
    public BargainLog queryByPrimaryKey(Long id) {
        return bargainLogMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<BargainLog> query(BargainLog bargainLog) {
        return bargainLogMapper.query(bargainLog);
    }

    @Override
    public PageResult<BargainLog> queryPage(BargainLog bargainLog, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<BargainLog> list = bargainLogMapper.queryPage(startIndex, pageQuery.getLimit(), bargainLog);
        long count = bargainLogMapper.queryPageCount(bargainLog);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public BigDecimal bargainMoneyCount(BargainLog bargainLog) {
        return bargainLogMapper.bargainMoneyCount(bargainLog);
    }

    @Override
    public PageResult<BargainLogRestResponse> listRestPage(MyBargainRestRequest myBargainRestRequest, Page pageQuery) {
        //计算下标
        BargainLog bargainLog = new BargainLog();
        bargainLog.setFightMasterId(myBargainRestRequest.getId());
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<BargainLogRestResponse> list = bargainLogMapper.listRestPage(startIndex, pageQuery.getLimit(), bargainLog);
        long count = bargainLogMapper.listRestPageCount(bargainLog);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(item -> {
                RestResult<MememberUserResponse> mememberUserResponseRestResult = mememberApi.queryMemberUserInfo(item.getMemberId());
                if (null != mememberUserResponseRestResult && null != mememberUserResponseRestResult.getData()) {
                          item.setHeadImg(mememberUserResponseRestResult.getData().getHeadImage());
                          item.setMemberName(mememberUserResponseRestResult.getData().getNickName());
                }
            });
        }
        ;
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
}